<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-row style="border-bottom: 1.5px solid #f4f6f8;">
        <el-row>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <el-form-item label="企业名称">
                <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" style="width: 200px;" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple-light">
              <el-form-item label="缴费时间：">
                <el-date-picker
                  v-model="value1"
                  style="width: 200px;"
                  type="daterange"
                  value-format="yyyy-MM-dd "
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple-light"><el-form-item>
              <el-button type="primary" @click="search">查询</el-button>
            </el-form-item></div>
          </el-col>
        </el-row>
      </el-row>
    </el-form>

    <!-- 按钮 -->
    <div>
      <el-row style="margin-top: 10px;margin-bottom: 10px;">
        <el-button type="primary" @click="add('add')">添加账单</el-button>
      </el-row>
    </div>

    <!-- 列表 -->
    <div>
      <el-row>
        <el-table
          style="width: 100%"
          :data="tableData"
          :header-cell-style="{backgroundColor:'#f1f1f1'}"
        >
          <el-table-column type="index" label="序号" width="100" />
          <!--  -->
          <el-table-column prop="billNumber" label="账单编号" />
          <el-table-column prop="buildingName" label="企业名称" />
          <el-table-column prop="buildingId" label="租赁楼宇" />
          <el-table-column prop="propertyFeePrice" label="物业费(元/㎡)" />
          <el-table-column prop="paymentAmount" label="账单金额(元)" />
          <el-table-column prop="createTime" label="缴费时间" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-link type="primary" @click="editA('type',row)"> 查看</el-link>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <el-link type="primary" @click="del(row.id)">删除</el-link>
            </template>

          </el-table-column>

        </el-table>
      </el-row>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="form.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="+total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog :visible="dialogVisible" width="480px" :title="title" @close="closeDialog">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" label-width="100px">
          <el-form-item label="选择租户" prop="enterpriseId">
            <el-select v-model="addForm.enterpriseId" style="width: 320px" placeholder="请选择租户">
              <el-option v-for="item in getLipropetyListst" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="租赁楼宇" prop="buildingId">
            <el-select v-model="addForm.buildingId" style="width: 320px" placeholder="请选择租赁楼宇">
              <el-option v-for="item in build" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="缴费周期" prop="time">
            <el-date-picker
              v-model="time"
              style="width: 320px;"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @blur="blurtime"
            />
          </el-form-item>
          <el-form-item label="支付金额" prop="paymentAmount">
            <el-input v-model="addForm.paymentAmount" style="width: 320px" />
          </el-form-item>
          <el-form-item label="支付方式" prop="paymentMethod">
            <el-select v-model="addForm.paymentMethod" style="width: 320px" placeholder="请选择租赁楼宇">
              <el-option v-for="item in methodList" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>

          </el-form-item>

        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="closeDialog">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>

    <el-dialog :visible="DialogTwo" width="480px" title="查看" @close="closeDialogA">
      <div style="height: 400px;font-size: 20px;display: flex;flex-direction: column;justify-content: space-around;">
        <el-row>
          <el-col :span="8" style="text-align: right;"><div class="grid-content bg-purple">租户名称：</div></el-col>
          <el-col :span="16"><div class="grid-content bg-purple-light">{{ edit.enterpriseId }}</div></el-col>
        </el-row>
        <el-row>
          <el-col :span="8" style="text-align: right;"><div class="grid-content bg-purple">租赁楼宇：</div></el-col>
          <el-col :span="16"><div class="grid-content bg-purple-light">{{ edit.buildingId }}</div></el-col>
        </el-row>
        <el-row>
          <el-col :span="8" style="text-align: right;"><div class="grid-content bg-purple">缴费周期</div></el-col>
          <el-col :span="16"><div class="grid-content bg-purple-light">{{ edit.startTime-edit.endTime }}</div></el-col>
        </el-row>
        <el-row>
          <el-col :span="8" style="text-align: right;"><div class="grid-content bg-purple">物业费(元/㎡)</div></el-col>
          <el-col :span="16"><div class="grid-content bg-purple-light">{{ edit.propertyFeePrice }}</div></el-col>
        </el-row>
        <el-row>
          <el-col :span="8" style="text-align: right;"><div class="grid-content bg-purple">账单金额(元)</div></el-col>
          <el-col :span="16"><div class="grid-content bg-purple-light">{{ edit.paymentAmount }}</div></el-col>
        </el-row>
        <el-row>
          <el-col :span="8" style="text-align: right;"><div class="grid-content bg-purple">支付方式</div></el-col>
          <el-col :span="16"><div class="grid-content bg-purple-light">{{ edit.paymentMethod }}</div></el-col>
        </el-row>
        <el-row>
          <el-col :span="8" style="text-align: right;"><div class="grid-content bg-purple">缴费时间</div></el-col>
          <el-col :span="16"><div class="grid-content bg-purple-light">{{ edit.createTime }}</div></el-col>
        </el-row>
      </div>
      <template #footer>
        <el-button size="mini" @click="closeDialogA">取 消</el-button>
        <el-button size="mini" type="primary" @click="closeDialog">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getList, buildList, priceNum, addList, delList } from '@/api/propety'
// 枚举值
import { propetyList, methodList } from '@/api/rodManageList'
export default {
  name: 'Propety',
  data() {
    return {
      DialogTwo: false,
      value1: '',
      form: {
        page: 1,
        pageSize: 10,
        enterpriseName: '',
        start: '',
        end: ''
      },
      tableData: [],
      total: '',
      addForm: {
        id: '',
        enterpriseId: '',
        buildingId: '',
        startTime: '',
        endTime: '',
        paymentAmount: '',
        paymentMethod: ''
      },
      dialogVisible: false,
      getLipropetyListst: [],
      build: [],
      time: '',
      methodList: [],
      type: '',
      startTimeList: [],
      edit: {
        enterpriseId: '',
        buildingId: '',
        startTime: '',
        endTime: '',
        propertyFeePrice: '',
        paymentAmount: '',
        paymentMethod: '',
        createTime: ''

      }
    }
  },
  computed: {
    title() {
      if (this.type === 'add') {
        return '添加'
      } else {
        return '编辑'
      }
    }
  },
  created() {
    this.search()
    this.LipropetyListst()
  },
  methods: {
    // 删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delList(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.search()
      })
    },
    // 时间失去焦点事件
    async blurtime() {
      console.log(this.time)
      this.addForm.startTime = this.time[0]
      this.addForm.endTime = this.time[1]
      if (this.addForm.buildingId && this.time) {
        const res = await priceNum({
          buildingId: this.addForm.buildingId,
          startTime: this.addForm.startTime,
          endTime: this.addForm.endTime
        })
        // console.log(res)

        this.addForm.paymentAmount = res.data
      }
    },
    // 点击确定
    submit() {
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        // console.log(2)
        await addList(this.addForm)
        this.$message.success('添加成功')
        this.dialogVisible = false
        this.$refs.addForm.resetFields()
        this.time = ''
        this.search()
      })
    },
    async LipropetyListst() {
      this.methodList.push(...methodList())
      this.getLipropetyListst.push(...propetyList())
      const res = await buildList()
      this.build = res.data
    },
    //
    // 关闭弹窗
    closeDialogA() {
      this.DialogTwo = false
      this.edit = {}
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
      this.time = ''
    },
    // 点击编辑打开弹窗
    async editA(type, row) {
      this.type = type
      this.edit = row
      this.DialogTwo = true
    },
    // 打开弹窗
    add(type) {
      this.type = type
      this.dialogVisible = true
    },
    handleSizeChange(val) {
      this.form.pageSize = val
      this.search()
    },
    handleCurrentChange(val) {
      this.form.page = val
      this.search()
    },
    async search() {
      this.form.start = this.value1[0]
      this.form.end = this.value1[1]
      const res = await getList(this.form)
      // console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
<style scoped  lang="scss">
.block{
  text-align: right;
}
</style>

